package com.zHan.sky.controller.User;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zHan.sky.common.result.Result;
import com.zHan.sky.dto.DishDto;
import com.zHan.sky.entity.Dish;
import com.zHan.sky.entity.Setmeal;
import com.zHan.sky.entity.SetmealDish;
import com.zHan.sky.service.DishService;
import com.zHan.sky.service.SetmealDishService;
import com.zHan.sky.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐管理
 */
@Slf4j
@RestController("userSetmealController")
@RequestMapping("/user/setmeal")
@Api(tags = "C端-套餐相关接口")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private DishService dishService;


    /**
     * 根据条件查询套餐数据
     *
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache", key = "#setmeal.categoryId")
    @ApiOperation(value = "根据条件查询套餐数据")
    public Result<List<Setmeal>> list(Setmeal setmeal) {
        //条件构造器
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        //过滤条件
        queryWrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId,
                        setmeal.getCategoryId())
                .eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus())
                .orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> setmealList = setmealService.list(queryWrapper);

        return Result.success(setmealList);
    }

    /**
     * 查看套餐具体内容
     *
     * @param id
     * @return
     */
    @GetMapping("/dish/{id}")
    @ApiOperation(value = "查看套餐具体内容")
    public Result<List<DishDto>> dish(@PathVariable Long id) {
        //条件构造器
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        //过滤条件
        queryWrapper.eq(SetmealDish::getSetmealId, id);
        //执行方法
        List<SetmealDish> setmealDishList = setmealDishService.list(queryWrapper);

        List<DishDto> dishDtoList = setmealDishList.stream().map((item) -> {
            //创建dishDto对象
            DishDto dishDto = new DishDto();
            //查询dish的id
            Long dishId = item.getDishId();
            //根据id查询dish
            Dish dish = dishService.getById(dishId);

            //对象拷贝
            BeanUtils.copyProperties(dish, dishDto);
            dishDto.setCopies(item.getCopies());

            return dishDto;
        }).collect(Collectors.toList());

        return Result.success(dishDtoList);

    }

}
